<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="购买厂家" prop="supplier">
        <el-input v-model="queryParams.supplier" placeholder="请输入购买厂家" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入设备名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入设备编码" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备品牌" prop="pinpai">
        <el-input v-model="queryParams.pinpai" placeholder="请输入设备品牌" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="型号" prop="xinghao">
        <el-input v-model="queryParams.xinghao" placeholder="请输入型号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="规格" prop="guige">
        <el-input v-model="queryParams.guige" placeholder="请输入规格" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="购买日期" prop="buyDate">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="录入日期" prop="buyDate">
        <el-date-picker v-model="createDateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="位置" prop="addr">
        <el-input v-model="queryParams.addr" placeholder="请输入位置" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产能" prop="channeng">
        <el-input v-model="queryParams.channeng" placeholder="请输入产能" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="所属车间" prop="haveHouse">
        <el-input v-model="queryParams.haveHouse" placeholder="请输入所属车间" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="报工状态" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.shebei_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="用于" prop="yongyu">
        <el-input v-model="queryParams.yongyu" placeholder="请输入用于" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['develop:LnShebei:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
          <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini"
                     :disabled="multiple" v-hasPermi="['develop:LnShebei:remove']">删除
          </el-button>
        </el-popconfirm>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['develop:LnShebei:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table" :max-height="tableMaxHeight" @sort-change="handleSortChange" show-summary :summary-method="getSummaries">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column sortable label="序号" align="center" prop="idx"/>
      <el-table-column sortable label="购买厂家" align="center" prop="supplier"/>
      <el-table-column sortable label="设备名称" align="center" prop="name"/>
      <el-table-column sortable label="设备编码" align="center" prop="code"/>
      <el-table-column sortable label="设备品牌" align="center" prop="pinpai"/>
      <el-table-column sortable label="型号" align="center" prop="xinghao"/>
      <el-table-column sortable label="规格" align="center" prop="guige"/>
      <el-table-column sortable label="购买日期" align="center" prop="buyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="录入日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column sortable label="位置" align="center" prop="addr"/>
      <el-table-column sortable label="产能" align="center" prop="channeng"/>
      <el-table-column sortable label="设备类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shebei_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="所属车间" align="center" prop="haveHouse"/>
      <el-table-column prop="fapiao" label="发票" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.fapiao"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column prop="hetong" label="合同" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.hetong"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column prop="baogao" label="检定证书" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.baogao"></file-upload-show>
        </template>
      </el-table-column>

      <el-table-column label="单价(元)" align="center" prop="money"/>
      <el-table-column label="数量" align="center" prop="counts"/>
      <el-table-column label="总金额(元)" align="center" prop="allmoney"/>
      <el-table-column label="设备审核状态" align="center" prop="shenhestatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shebei_status" :value="scope.row.shenhestatus" />
        </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="createBy"/>
      <el-table-column label="审核人" align="center" prop="shenheName"/>
      <el-table-column label="经办人" align="center" prop="usename"/>
      <el-table-column prop="mingpai" label="铭牌" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.mingpai"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column prop="pictrue" label="照片" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.pictrue"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="用途" align="center" prop="yongyu"/>
      <!--   <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-popconfirm :title="`确认审核通过当前数据项？`" @confirm="shenbeishenhe(scope.row)" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:LnShebei:shenhe']">审核通过
            </el-button>
          </el-popconfirm>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['develop:LnShebei:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="maintenanceBnt(scope.row)"
                    >设备维修保养
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:LnShebei:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <lnShebei-form ref="form" @ok="getList"></lnShebei-form>
    <maintenance-list ref="maintenanceList" @ok="getList"></maintenance-list>
  </div>
</template>

<script>
import {deleteAction, getAction,accAdd} from '@/api/manage'
import LnShebeiForm from '@/views/develop/lnShebei/module/LnShebeiForm'
import maintenanceList from '@/views/develop/lnShebei/module/maintenanceList'
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  name: "LnShebeiList",
  dicts: ['shebei_type','shebei_status'],
  components: {
    FileUploadShow,
    LnShebeiForm,
    maintenanceList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      dataList: [],
      dateRange: [],
      createDateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        pinpai: null,
        xinghao: null,
        guige: null,
        buyDate: null,
        addr: null,
        channeng: null,
        status: null,
        haveHouse: null,
        type: null,
        supplier: null,
      },
      url: {
        list: '/lnShebei/lnShebei/list',
        remove: '/lnShebei/lnShebei/',
        export: '/lnShebei/lnShebei/export',
        shenhe: '/lnShebei/lnShebei/shenhe/',
      },
      tableMaxHeight: window.innerHeight - 320,
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.queryParams.type = this.$route.query.type;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    maintenanceBnt(row) {
      this.$refs.maintenanceList.handleAdds(row);
    },
    shenbeishenhe(row) {
      const id = row.id;
      deleteAction(this.url.shenhe, id).then(response => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {
      });
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "counts" || column.property === "allmoney" || column.property === "money") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange,'BuyDate');
      this.addDateRange(this.queryParams, this.createDateRange,'CreateTime');
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `lnShebei_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
